package com.koocloud.electroplatemanage.utils;

import jxl.Sheet;
import jxl.Workbook;

import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @program: electroplatemanage
 * @description: Excel导入工具类
 * @author: zww
 * @create: 2020-10-26 13:19
 */
public class ExcelImportUtils extends JFrame implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {


    }
    public static Object getPathSuccess() {

        try {
            //获取桌面路径
            FileSystemView fsv = FileSystemView.getFileSystemView();
            JFileChooser jfc=new JFileChooser(fsv.getHomeDirectory());
            //打开选择框
            jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
            int result = jfc.showDialog(new JLabel(), "Excel导入");
            File file=jfc.getSelectedFile();

            // 当用户选择撤销时，直接返回
            if (result != 0 || file == null) {
                JOptionPane.showMessageDialog(null, "请选择Excel文件", "提示！", 1);
                return "1";

            }
                // 判断选择文件类型是否是Excel格式
                String path = file.getPath();

                if (!path.endsWith(".xls") && !path.endsWith(".xlsx")) {
                    JOptionPane.showMessageDialog(null, "请选择Excel格式的文件！", "提示！", 1);
                    throw new Exception("请选择Excel格式的文件！");
                }

                // 得到Excel文件对象
                Workbook book = Workbook.getWorkbook(file);
                Sheet seData = book.getSheet("11");

                if (seData == null) {
                    JOptionPane.showMessageDialog(null, "Excel中没有对应页签,无法完成导入!", "提示！", 1);
                    throw new Exception("Excel中没有对应页签,无法完成导入!");
                }
                int rownum = seData.getRows();
                if (rownum <= 0) {
                    JOptionPane.showMessageDialog(null, "没有需要导入的数据!", "提示！", 1);
                    return "1";
                }
                List<LinkedHashMap> dataList = new ArrayList<LinkedHashMap>();

            // 循环插入数据
/*

            for (int i = 3; i < rownum; i++) {

                String cmaterialvidcode = seData.getCell(1, i).getContents(); // 物料编码
                if (cmaterialvidcode == null || cmaterialvidcode.equals("")) {
                    MessageDialog.showHintDlg((Container) editor, "提示",
                            "物料编码为必输项，请检查Excel导入模板物料编码字段的第" + excelrow
                                    + "行是否填入数据！");
                    throw new BusinessException("Excel导入模板物料编码为空!");
                }


            }
*/



                return dataList;

        } catch(Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "异常："+e.getMessage(), "提示！", 1);
            return "1";
        }
    }

    public static void main(String[] args) {
        getPathSuccess();
    }
}